package base

import "fmt"

func (conf *Config) GetDevLoopBack0Ip(devName string) string {
	for _, aa := range conf.AsAreas {
		for _, dia := range aa.DevInAs {
			if devName == dia.Dev {
				for _, id := range dia.Intfs {
					if id.Port == "loopback0" {
						return id.PortIp
					}
				}
			}
		}
	}
	panic(fmt.Sprintf("can't find loopback for %s", devName))
}

func (conf *Config) GetDevAs(devName string) int {
	for _, aa := range conf.AsAreas {
		for _, dia := range aa.DevInAs {
			if dia.Dev == devName {
				return aa.As
			}
		}
	}
	panic(fmt.Sprintf("can't find devAs for %s", devName))
}

// 寻找对端设备的名称和端口Ip
func (conf *Config) FindPeerDev(devName string, port string, ip string) (string, string) {
	for _, l := range conf.Links {
		if l.Peer1.Dev == devName && l.Peer1.PortIp == ip {
			return l.Peer2.Dev, l.Peer2.PortIp
		}
		if l.Peer2.Dev == devName && l.Peer2.PortIp == ip {
			return l.Peer1.Dev, l.Peer1.PortIp
		}
	}
	return "", ""
}

func (conf *Config) FindDevIntf(devName string, ip string) string {
	for _, aa := range conf.AsAreas {
		for _, dia := range aa.DevInAs {
			if dia.Dev == devName {
				for _, id := range dia.Intfs {
					if id.PortIp == ip {
						return id.Port
					}
				}
			}
		}
	}
	panic(fmt.Sprintf("can't find dev intf for %s-%s", devName, ip))
}
